def prost(x):

    k = 0

    for d in range(1,x+1):

        if x%d==0:

            k+=1

    if k == 2:

        return True

    else:

        return False

maxx = 0

num = int(input())

if num >= 1000 and num <= 9999:

    n1 = str(num)

    for cifa in range(len(n1)):

        if prost(int(n1[cifa])) == True:

           maxx = max(maxx,int(n1[cifa]))

    for chislo in range(len(n1)-1):

        if prost(int(n1[chislo]+n1[chislo+1])) == True:

           maxx = max(maxx,int(n1[chislo]+n1[chislo+1]))

    for chislo in range(len(n1)-2):

        if prost(int(n1[chislo]+n1[chislo+1]+n1[chislo+2])) == True:

           maxx = max(maxx,int(n1[chislo]+n1[chislo+1]+n1[chislo+2]))

    for chislo in range(len(n1)-3):

        if prost(int(n1[chislo]+n1[chislo+1]+n1[chislo+2]+n1[chislo+3])) == True:

           maxx = max(maxx,int(n1[chislo]+n1[chislo+1]+n1[chislo+2]+n1[chislo+3]))
    

    print(maxx)

else:

    print(0)
